package org.software.views.group;
import org.software.controller.GroupController;
import org.software.views.MenuFrame;

import javax.swing.*;
import java.awt.*;

public class GroupFrame extends JFrame {
    private JTextField groupNameField;
    private JTextField classNameField;

    public GroupFrame() {
        setTitle("新增小组");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridLayout(3, 2));
        JLabel groupNameLabel = new JLabel("小组");
        groupNameField = new JTextField(15);
        JLabel classNameLabel = new JLabel("班级");
        classNameField = new JTextField(15);
        JButton addButton = new JButton("添加");
        JButton backButton = new JButton("返回");
        setLayout(new FlowLayout(FlowLayout.CENTER));
        add(groupNameLabel);
        add(groupNameField);
        add(classNameLabel);
        add(classNameField);
        add(addButton);
        add(backButton);
        addButton.addActionListener(e -> {
            String groupName = groupNameField.getText().trim();
            String className = classNameField.getText().trim();
            if (groupName.isEmpty()|| className.isEmpty()) {
                JOptionPane.showMessageDialog(GroupFrame.this,"小组和班级不能为空","警告",JOptionPane.WARNING_MESSAGE);
            }else {
                GroupController groupController = new GroupController();
                boolean isAdded =groupController.groupAdd(groupName,className);
                if (isAdded){
                    JOptionPane.showMessageDialog(GroupFrame.this,"添加成功");
                }else{
                    JOptionPane.showMessageDialog(GroupFrame.this,"添加失败，小组名称可能已存在");

                }
            }
        });
        backButton.addActionListener(e ->
        {   dispose();
            new MenuFrame();
        });
        setVisible(true);

    }
}
